package com.springpad;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.database.NotebookAdapter;
import com.model.Notebook;

import firstdroid.tutorial.gridview.R;

public class MyGridView extends Activity {
	GridView MyGrid;
	static int len;
	AlertDialog.Builder alert;
	private Context ctx;
	NotebookAdapter na;
	Integer notebookid;
	int temp;
	ArrayList<Notebook> groupsList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ctx = this;
		na = new NotebookAdapter(getApplicationContext());
		refreshList();
		MyGrid = (GridView) findViewById(R.id.MyGrid);
		MyGrid.setAdapter(new ImageAdapter(this));
		MyGrid.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final int position=arg2;
				//Log.d("position arg", position+"");
				alert=new AlertDialog.Builder(ctx);
				alert.setTitle("Delete NoteBook");
		    	alert.setMessage("Confrim delete NoteBook "+groupsList.get(arg2).getName());
		    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						deleteNotebook(position);
						startActivity(new Intent(MyGridView.this,
								MyGridView.class));
						
					}
				});
		    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		    	alert.create().show();
				return true;
			}
			
		});
		MyGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
			    temp=position;
				if (position == (len - 1)) {

					alert = new AlertDialog.Builder(ctx);
					alert.setTitle("Add a New Notebook");
					final EditText name = new EditText(MyGridView.this);
					alert.setView(name);
					alert.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									String notename = name.getText().toString();
									createNewNotebook(notename, 0);
									refreshList(); 
									notebookid=findNotebookId(temp);
									Intent i=new Intent(MyGridView.this,Menu.class);
									i.putExtra("notebookid",notebookid.toString());
									startActivity(i);
									//startActivity(new Intent(MyGridView.this,Menu.class));
								}
							});
					alert.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {

								}
							});
					alert.create().show();

					// Toast.makeText(MyGridView.this, "" + position+"hiii",
					// Toast.LENGTH_SHORT).show();
				} else {
					//Toast.makeText(MyGridView.this, "" + len,
							//Toast.LENGTH_SHORT).show();
					//startActivity(new Intent(MyGridView.this,Menu.class));
					notebookid=findNotebookId(temp);
					Intent i=new Intent(MyGridView.this,Menu.class);
					i.putExtra("nbid",notebookid.toString());
					startActivity(i);
				}
			}

		});
		refreshList(); 
	}

	private void refreshList() {
		na.open();
		groupsList = na.fetchNotebook();
		 Log.d("count----->",groupsList.size()+"");
		// lv.setAdapter(new
		// GroupListAdapter(getApplicationContext(),groupsList));
		na.close();
	}

	private void createNewNotebook(String name, int item) {
		Notebook n = new Notebook();
		n.setName(name);
		n.setItems(item);
		na.open();
		na.createNoteBook(n);
		na.close();
	}
	
	protected int findNotebookId(Integer position){
		
		Integer groupId=groupsList.get(position).getId();
		return groupId;
	}
	
	
	protected void deleteNotebook(Integer position){
		Integer groupId=groupsList.get(position).getId();
		
		Log.d("posssssssss",groupId.toString());
		Log.d("posssssssss",position.toString());
		na.open();
		na.deleteNotebook(groupId);
		na.close();
		refreshList();
	}
	public class ImageAdapter extends BaseAdapter {
		Context MyContext;

		public ImageAdapter(Context _MyContext) {
			MyContext = _MyContext;
		}

		@Override
		public int getCount() {
			//len = 1;
			len=groupsList.size()+1;
			return len;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View MyView = convertView;
           Log.d("pos------->",position+"");
           Log.d("count----->",getCount()+"");
			if (convertView == null) {
				if (position < (getCount()-1 )) {
					LayoutInflater li = getLayoutInflater();
					MyView = li.inflate(R.layout.grid_item, null);

					TextView tv = (TextView) MyView
							.findViewById(R.id.textView1);
					tv.setText("Item " + position);
					TextView t = (TextView) MyView.findViewById(R.id.textView2);
					tv.setText(groupsList.get(position).getName());
					//t.setText(groupsList.get(position).getItems());
				} else {
					LayoutInflater li = getLayoutInflater();
					MyView = li.inflate(R.layout.additem, null);

				}

			}

			return MyView;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
	}
}